/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package walrus.text;

import java.io.File;
import java.util.concurrent.BlockingQueue;

import name.robertburrelldonkin.alfie.Actor;
import name.robertburrelldonkin.alfie.Conductor;
import name.robertburrelldonkin.alfie.Message;
import name.robertburrelldonkin.alfie.Pool;
import name.robertburrelldonkin.alfie.builders.EngineSinkBuilder;


import walrus.base.Constants;

public class TextBuilder {

	private String outputFile = null;
	private int buffers = Constants.DEFAULT_NUMBER_OF_BUFFERS;
	private boolean ignoreCase = true;
	private boolean small = false;
	
	public TextBuilder() {}

	public int getBuffers() {
		return buffers;
	}

	public TextBuilder setBuffers(int buffers) {
		this.buffers = buffers;
		return this;
	}

	public String getOutputFile() {
		return outputFile;
	}

	public TextBuilder setOutputFile(String outputFile) {
		this.outputFile = outputFile;
		return this;
	}

	public boolean isIgnoreCase() {
		return ignoreCase;
	}

	public TextBuilder setIgnoreCase(boolean ignoreCase) {
		this.ignoreCase = ignoreCase;
		return this;
	}

	public Conductor countEngine(Conductor conductor,
			BlockingQueue<Message<File>> fromSource) {
		if (small) {
			return smallCountEngine(conductor, fromSource);
		}
		return scalableCountEngine(conductor, fromSource);		
	}

	private Conductor scalableCountEngine(Conductor conductor,
			BlockingQueue<Message<File>> fromSource) {
		final Actor<File, WordCount> actor = new Pool<File, WordCount>(new GutenburgWordsActor.Factory(),buffers).open();
		final Actor<WordCount, WordCount> sinkActor = buildWordCounter();
		return new EngineSinkBuilder<File, WordCount>().build(conductor, fromSource,  actor, sinkActor);
	}

	
	private Conductor smallCountEngine(Conductor conductor,
			BlockingQueue<Message<File>> fromSource) {
		final Actor<File, WordCount> actor = new Pool<File, WordCount>(new GutenburgWordsActor.Factory(),buffers).open();
		final Actor<WordCount, WordCount> sinkActor = buildWordCounter();
		return new EngineSinkBuilder<File, WordCount>().build(conductor, fromSource,  actor, sinkActor);
	}

	private WordCounterActor buildWordCounter() {
		final WordCounterActor wordCounter = new WordCounterActor();
		if (outputFile != null) {
			wordCounter.setOutputFile(new File(outputFile));
		}
		wordCounter.setIgnoreCase(ignoreCase);
		return wordCounter;
	}

}
